﻿Imports Mystery
Imports System.IO
Imports System.Windows.Forms

Public Class StringDirectoryPathView
    Implements IPropertyControl


    Private _property_info As System.Reflection.PropertyInfo
    Private _content_property As ContentProperty

    Private _dir_info As DirectoryInfo

    Public Property property_info As System.Reflection.PropertyInfo Implements IPropertyControl.property_info
        Get
            Return _property_info
        End Get
        Set(value As System.Reflection.PropertyInfo)
            _content_property = value.getMysteryAttribute(Of ContentProperty)()
            _label = _content_property.label
            _property_info = value
        End Set
    End Property

    Private Function isNullOrEmpty() As Boolean
        Return _dir_info Is Nothing OrElse Not _dir_info.Exists
    End Function


    Public Property value As Object Implements IPropertyControl.value
        Get
            If _dir_info Is Nothing OrElse Not _dir_info.Exists Then Return ""
            Return _dir_info.FullName
        End Get
        Set(value As Object)
            If TypeOf (value) Is ContentPropertyValue(Of String) Then value = DirectCast(value, ContentPropertyValue).getValue
            Dim new_value As String = value
            If Not String.IsNullOrEmpty(new_value) Then
                _dir_info = New DirectoryInfo(new_value)
            Else
                _dir_info = Nothing
            End If

            lNotGiven.Visibility = If(Me.isNullOrEmpty(), Windows.Visibility.Visible, Windows.Visibility.Hidden)

            RaiseEvent valueChanged()
        End Set
    End Property


    Private _content As IContent

    Public Property content_item As Mystery.IContent Implements IPropertyControl.content
        Get
            Return _content
        End Get
        Set(value As Mystery.IContent)
            _content = value
            Me.value = _content_property.retrive()(_content)
        End Set
    End Property


    Private _label As String
    Public ReadOnly Property label As String Implements IPropertyControl.label
        Get
            Return _label
        End Get
    End Property

    Public ReadOnly Property wished_height As System.Windows.GridLength Implements IPropertyControl.wished_height
        Get
            Return New GridLength(100)
        End Get
    End Property

    Public Function setInContent() As IEnumerable(Of IContent) Implements IPropertyControl.setInContent
        Dim path As String = String.Empty
        If Not Me.isNullOrEmpty() Then
            path = _dir_info.FullName
        End If
        Dim v As New ContentPropertyValue(Of String)(path)
        _content_property.save()(_content, v)
        Return New List(Of IContent) From {_content}
    End Function

    Public Event valueChanged() Implements IPropertyControl.valueChanged

    Private Sub myButton_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles myButton.Click
        Dim dia As New FolderBrowserDialog
        dia.ShowNewFolderButton = True
        If Not Me.isNullOrEmpty() Then
            dia.SelectedPath = _dir_info.FullName
        End If
        If dia.ShowDialog() = DialogResult.OK Then
            Me.value = dia.SelectedPath
        End If
    End Sub

End Class
